package cn.lklink.pay.lkl.core;

import cn.lklink.pay.lkl.util.HttpUtil;
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 拉卡拉拓客token对象
 *
 * @author Mr丶xie
 * @version 1.0.0
 * @mail xielinke@lklink.cn
 */
public class TkToken {

    private static volatile TkToken token;

    private TkToken() {
    }

    public static TkToken getInstance() throws IOException {
        if (token == null) {
            synchronized (TkToken.class) {
                if (token == null) {
                    token = getToken();
                } else {
                    if (!token.verify()) {
                        token = getToken();
                    }
                }
            }
        } else {
            if (!token.verify()) {
                token = getToken();
            }
        }
        return token;
    }

    private static TkToken getToken() throws IOException {
        List<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("grant_type", "client_credentials"));
        list.add(new BasicNameValuePair("client_id", LakalaPayConfig.TK_CLIENT_ID));
        list.add(new BasicNameValuePair("client_secret", LakalaPayConfig.TK_CLIENT_SECRET));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", getAuthorization());

        HttpResponse response = HttpUtil.post(LakalaPayUrl.TK_OAUTH_TOKEN, entity, headerMap);
        String res = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
        token = new Gson().fromJson(res, TkToken.class);
        token.setCreateTokenTime(System.currentTimeMillis());
        return token;
    }


    private static String getAuthorization() throws UnsupportedEncodingException {
        String src = String.format("%s:%s", LakalaPayConfig.TK_CLIENT_ID, LakalaPayConfig.TK_CLIENT_SECRET);
        return "Basic " + Base64.getEncoder().encodeToString(src.getBytes("UTF-8"));
    }


    /**
     * 访问token
     */
    private String access_token;

    /**
     * token类型
     */
    private String token_type;

    /**
     * 过期时间，
     * 以当前时间为基准的秒数
     */
    private Long expires_in;

    /**
     * 有效范围
     */
    private String scope;

    /**
     * 获取token的时间
     */
    private Long createTokenTime;

    public boolean verify() {
        return System.currentTimeMillis() < createTokenTime + expires_in * 1000;
    }


    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getToken_type() {
        return token_type;
    }

    public void setToken_type(String token_type) {
        this.token_type = token_type;
    }

    public Long getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(Long expires_in) {
        this.expires_in = expires_in;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public Long getCreateTokenTime() {
        return createTokenTime;
    }

    public void setCreateTokenTime(Long createTokenTime) {
        this.createTokenTime = createTokenTime;
    }
}
